<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()"
             label-width="80px">
      <el-form-item label="创建人ID" prop="createUserId">
        <el-input v-model="dataForm.createUserId" placeholder="创建人ID"></el-input>
      </el-form-item>
      <el-form-item label="房间号" prop="no">
        <el-input v-model="dataForm.no" placeholder="房间号"></el-input>
      </el-form-item>
      <el-form-item label="快速进入地址" prop="link">
        <el-input v-model="dataForm.link" placeholder="快速进入地址"></el-input>
      </el-form-item>
      <el-form-item label="使用人数" prop="userCount">
        <el-input v-model="dataForm.userCount" placeholder="使用人数"></el-input>
      </el-form-item>
      <el-form-item label="使用时长" prop="useTime">
        <el-input v-model="dataForm.useTime" placeholder="使用时长"></el-input>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-input v-model="dataForm.endTime" placeholder="结束时间"></el-input>
      </el-form-item>
      <el-form-item label="是否开启" prop="open">
        <el-input v-model="dataForm.open" placeholder="是否开启"></el-input>
      </el-form-item>
      <el-form-item label="房间类型:0->一对一;1->一对多" prop="type">
        <el-input v-model="dataForm.type" placeholder="房间类型:0->一对一;1->一对多"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          createTime: '',
          deleted: '',
          version: '',
          lastModifyTime: '',
          createUserId: '',
          no: '',
          link: '',
          userCount: '',
          useTime: '',
          endTime: '',
          open: '',
          type: ''
        },
        dataRule: {
          createUserId: [
            {required: true, message: '创建人ID不能为空', trigger: 'blur'}
          ], no: [
            {required: true, message: '房间号不能为空', trigger: 'blur'}
          ], link: [
            {required: true, message: '快速进入地址不能为空', trigger: 'blur'}
          ], userCount: [
            {required: true, message: '使用人数不能为空', trigger: 'blur'}
          ], useTime: [
            {required: true, message: '使用时长不能为空', trigger: 'blur'}
          ], endTime: [
            {required: true, message: '结束时间不能为空', trigger: 'blur'}
          ], open: [
            {required: true, message: '是否开启不能为空', trigger: 'blur'}
          ], type: [
            {required: true, message: '房间类型:0->一对一;1->一对多不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/manage/room/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 200) {
                Object.assign(this.dataForm, data.data)
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/manage/room/${!this.dataForm.id ? 'save' : 'update'}`),
              method: `${!this.dataForm.id ? 'post' : 'put'}`,
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'createUserId': this.dataForm.createUserId,
                'no': this.dataForm.no,
                'link': this.dataForm.link,
                'userCount': this.dataForm.userCount,
                'useTime': this.dataForm.useTime,
                'endTime': this.dataForm.endTime,
                'open': this.dataForm.open,
                'type': this.dataForm.type
              })
            }).then(({data}) => {
              if (data && data.code === 200) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
